package com.koron.css2.systemManage.servlet;

import com.koron.css2.ApplicationConfig;
import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.css2.systemManage.bean.OrgUserBean;
import com.koron.css2.systemManage.bean.OrgUserQueryBean;
import com.koron.css2.systemManage.mapper.OrgUserMapper;
import com.koron.css2.systemManage.service.IDataRangeService;
import com.koron.css2.systemManage.service.ServiceFactory;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

/**
 * 组织用户删除
 * @author admin
 * @since 2021.07.27
 * @version 1.0
 *
 */
public class OrgUserDelete implements ServerInterface{
	
	private static Logger log = LogManager.getLogger(OrgUserDelete.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		MessageBean<String> message = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "删除成功", String.class);
		try {
			OrgUserQueryBean user = JsonUtils.objectToPojo(req.getData(), OrgUserQueryBean.class);
			if(StringUtils.isEmpty(user.getAccount())) {
				message.setCode(Constant.MESSAGE_INT_FAIL);
				message.setDescription("用户账号不能为空");
				return message;
			}
			OrgUserMapper mapper = factory.getMapper(OrgUserMapper.class);
			OrgUserBean orgUser = mapper.selectByAccount(user.getAccount());
			int iCount = mapper.deleteByAccount(user.getAccount());
			if(iCount > 0) {
				IDataRangeService dataRangeService = ServiceFactory.getDataRangeService();
				dataRangeService.deleteUserRangeValue(ApplicationConfig.getAppCustomerAppid(), orgUser.getCode(), "company", userInfo.getCurWaterCode());
				log.info("账号 {} 删除成功，操作人：{}，水司code：{}", user.getAccount(), userInfo.getUserInfo().getName(), userInfo.getCurWaterCode());
			}
		}catch(Exception ex) {
			log.error("删除失败，操作人：{}, 原因：{} ", userInfo.getUserInfo().getName(), ex);
			message.setCode(Constant.MESSAGE_INT_FAIL);
			message.setDescription("操作异常");
		}
		return message;
	}
}
